<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          placeholder="请输入菜单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['phoneMenu:menu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['phoneMenu:menu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['phoneMenu:menu:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['phoneMenu:menu:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="menuList" @selection-change="handleSelectionChange"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="菜单名称" align="center" prop="menuName" />
      <el-table-column label="图标路径" align="center" prop="menuPicPath" />
      <el-table-column label="菜单链接" align="center" prop="menuUrl" />
      <el-table-column label="菜单备注" align="center" prop="menuMark" />
      <el-table-column label="是否是菜单" align="center" prop="isMenu" :formatter="isMenuFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-add"
            @click="handleAdd(scope.row)"
            v-hasPermi="['phoneMenu:menu:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['phoneMenu:menu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['phoneMenu:menu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手机菜单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <!--<el-form-item label="上级菜单">
              <el-select style="width: 240px" :disabled="parentCantChange" v-model="form.parentId" placeholder="请选择">
                    <el-option
                    v-for="m in mainMenu"
                    :key="m.id"
                    :label="m.menuName"
                    :value="m.id"
                    ></el-option>
                </el-select>
        </el-form-item> -->
        <el-form-item label="图标" >
          <el-upload
              ref="upload"
              :headers="upload.headers"
              :action="upload.url"
              :disabled="upload.isUploading"
              :on-progress="handleFileUploadProgress"
              :on-success="handleFileSuccess"
              :on-remove="handleRemoveFile"
              :file-list="form.fileInfoList"
          >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="菜单链接" prop="menuUrl" >
          <el-input v-model="form.menuUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="菜单备注" prop="menuMark" >
          <el-input v-model="form.menuMark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMenu, getMenu, delMenu, addMenu, updateMenu, exportMenu ,getMainMenu} from "@/api/product/phoneMenu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getToken } from "@/utils/auth";
export default {
  name: "",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 手机菜单管理表格数据
      menuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否是菜单字典
      isMenuOptions: [],
      isMenuShow:false,
      parentCantChange:false,
      mainMenu:[],
      // 查询参数
      queryParams: {
        parentId: undefined,
        menuName: undefined,
        menuPicPath: undefined,
        menuUrl: undefined,
        menuMark: undefined,
        isMenu: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      upload: {
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/upload"
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.isMenuOptions = response.data;
    });
    this.getMainList();
  },
  methods: {
    /** 查询手机菜单管理列表 */
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then(response => {
         this.menuList = this.handleTree(response.data, "id");
        //this.menuList = response.rows;
        //this.total = response.total;
        this.loading = false;
      });
    },
    getMainList(){
      getMainMenu().then(response => {
        this.mainMenu = response.data;
      });
    },
    // 是否是菜单字典翻译
    isMenuFormat(row, column) {
      return this.selectDictLabel(this.isMenuOptions, row.isMenu);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        parentId: 0,
        menuName: undefined,
        menuPicPath: undefined,
        menuUrl: undefined,
        menuMark: undefined,
        isMenu: "0",
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        delFlag: undefined,
        orderNum:undefined,
        fileInfoList:[]
      };
      this.resetForm("form");
      this.parentCantChange = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if(row.id){
          this.form.parentId = row.id;
      }
      //this.getMainList();
      this.open = true;
      this.title = "添加手机菜单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMenu(id).then(response => {
        this.form = response.data;
        this.parentCantChange = true;
        this.open = true;
        this.title = "修改手机菜单管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMenu(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addMenu(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除手机菜单管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMenu(ids);
        }).then((data) => {
          if(data.msg == 'child'){
            this.msgInfo("存在子菜单，无法删除");
          }else{
            this.getList();
            this.msgSuccess("删除成功");
          }
         
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有手机菜单管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMenu(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
        this.upload.isUploading = false;
        this.form.menuPicPath =  fileList[0].response.fileName;
         for(var i in fileList){
              fileList[i].url = fileList[i].response.url;
              fileList[i].fileName = fileList[i].response.fileName;
          }
        this.form.fileInfoList = fileList;
    }
  }
};
</script>